﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;
using System.ComponentModel;
using WPFDiagramming.Interfaces;
using WPFDiagramming.Designer;
using System.Windows.Input;
using WPFDiagramming.Adorners;

namespace WPFDiagramming.Controls
{
    public class Connector : ControlBase
    {
        public Entity StartEntity { get; set; }
        public Entity EndEntity { get; set; }

        #region Fields

        private double _startRelativeTopPosition;
        private double _startRelativeLeftPosition;

        private double _endRelativeTopPosition;
        private double _endRelativeLeftPosition;

        #endregion

        public Connector()
        {
        }

        public Connector(Entity source, Entity destine, Point pStart, Point pEnd)
            : this()
        {
            this.StartEntity = source;
            this.EndEntity = destine;

            this.StartEntity.PropertyChanged += OnEntityPropertyChanged;
            this.EndEntity.PropertyChanged += OnEntityPropertyChanged;

            source.Connector = this;
            destine.Connector = this;

            if (pStart.X < (this.EndEntity.Width / 2))
                _startRelativeLeftPosition = 0;
            else
                _startRelativeLeftPosition = 1;

            _startRelativeTopPosition = (pStart.Y) / this.StartEntity.Height;


            if (pEnd.X < (this.EndEntity.Width / 2))
                _endRelativeLeftPosition = 0;
            else
                _endRelativeLeftPosition = 1;

            _endRelativeTopPosition = (pEnd.Y) / this.EndEntity.Height;
        }

        private void OnEntityPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.InvalidateVisual();
        }

        protected override void OnSelected(object sender, RoutedEventArgs e)
        {
            AdornerLayer layer = AdornerLayer.GetAdornerLayer(this);
            layer.Add(new ConnectorAdorner(this));

            Keyboard.Focus((UIElement)sender);
        }

        public override void RemoveAdorners()
        {
            base.RemoveAdorners();

            this.BorderBrush = Brushes.Black;

            this.InvalidateVisual();
        }

        protected override Geometry DrawGeometry()
        {
            Point startpoint = new Point(this.StartEntity.Left + (this.StartEntity.Width * _startRelativeLeftPosition), this.StartEntity.Top + (this.StartEntity.Height * _startRelativeTopPosition));
            Point endpoint = new Point(this.EndEntity.Left + (this.EndEntity.Width * _endRelativeLeftPosition), this.EndEntity.Top + (this.EndEntity.Height * _endRelativeTopPosition));

            PointOrientation startX = PointOrientation.Left;
            PointOrientation endX = PointOrientation.Left;

            if (_startRelativeLeftPosition == 1)
                startX = PointOrientation.Right;

            if (_endRelativeLeftPosition == 1)
                endX = PointOrientation.Right;

            this.Designer = new ConnectorDesigner(startpoint, startX, endpoint, endX);

            return this.Designer.Draw();
        }

    }
}
